<?php
if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class M_dokter extends CI_Model {

	public function __construct() {
		parent::__construct();
	}

	function get($kd_ppk='') {
		$sql = 'SELECT kd_dokter, dokter
		 FROM dokter
		 WHERE kd_ppk="'.$kd_ppk.'"
		 ORDER BY dokter';
        return $sql;

	}

	function cek1($rule1) {
		$sql = 'SELECT * FROM dokter WHERE kd_dokter=?';
		$query = $this->db->query($sql, $rule1);
        return $query;
	}

	function cek2($rule2) {
		$sql = 'SELECT * FROM dokter WHERE dokter=? AND kd_ppk=?';
		$query = $this->db->query($sql, $rule2);
        return $query;
	}

	function cekUpdate($rule) {
		$sql = 'SELECT * FROM dokter WHERE (kd_dokter=?) AND kd_dokter<>?';
		$query = $this->db->query($sql, $rule);
        return $query;
	}

	function get_cari($keyword='',$kd_ppk='') {
		$sql = 'SELECT kd_dokter, dokter
		 FROM dokter o
		 LEFT JOIN ppk ON o.kd_ppk=ppk.kd_ppk
		 WHERE (dokter like "%'.$keyword.'%" OR kd_dokter like "%'.$keyword.'%")
		 AND o.kd_ppk='.$kd_ppk.'
		 ORDER BY dokter';
		return $sql;
	}

	function get_export($params) {
		$sql = 'SELECT kd_dokter, dokter,ppk
		 FROM dokter o
		 LEFT JOIN ppk ON o.kd_ppk=ppk.kd_ppk
		 WHERE o.kd_ppk = ?
		 ORDER BY dokter';
		$query = $this->db->query($sql,$params);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function get_ket($params) {
		$sql = 'SELECT dokter FROM dokter WHERE kd_dokter = ?';
		$query = $this->db->query($sql, $params);
		if ($query->num_rows() > 0) {
			$result = $query->row_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result['dokter'];
	}

	

	function getPPK($params) {
		$sql = 'SELECT * FROM ppk
		 WHERE kd_ppk= ?';
		$query = $this->db->query($sql, $params);
		if ($query->num_rows() > 0) {
			$result = $query->row_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function save($dataInsert) {
		$query = $this->db->insert('dokter', $dataInsert);
		return $query;
	}

	function get_by($params) {
		$sql = 'SELECT kd_dokter, dokter,ppk,
		 o.kd_ppk AS KDPPK
		 FROM dokter o
		 LEFT JOIN ppk ON o.kd_ppk=ppk.kd_ppk
		 WHERE o.kd_dokter = ?';
		$query = $this->db->query($sql, $params);
		if ($query->num_rows() > 0) {
			$result = $query->row_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function update($kd_dokter, $dataUpdate) {
		$this->db->where('kd_dokter', $kd_dokter);
		$update = $this->db->update('dokter', $dataUpdate);
		return $update;
	}

	function delete($kd_dokter) {
		$this->db->where('kd_dokter', $kd_dokter);
		$delete = $this->db->delete('dokter');
		return $delete;
	}
}
